package com.abc.common.web.context;

import com.abc.common.util.StringEscapeUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * Created by xuexue on 2014/4/27.
 */
public class LoginContext implements Serializable {
    private final static Logger log = LoggerFactory.getLogger(LoginContext.class);
    private static final ThreadLocal<LoginContext> CONTEXT_HOLDER=new ThreadLocal<LoginContext>();

    private static final ThreadLocal<String> SUBMIT_IP_HOLDER=new ThreadLocal<String>();
    /**
     * 用户ID
     */
    private String userId;
    /**
     * 登录名称
     */
    private String pin;

    /**
     * 显示名称
     */
    private String nick;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getPin() {
        return pin;
    }

    public void setPin(String pin) {
        this.pin = pin;
    }

    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }

    /**
     * 反向构造上下文。
     * @param value 需要反向构造的串。形式如下：userId=123,pin=yangsy,nick=杨思勇
     * @return 上下文
     */
    public static LoginContext parse(String value) {
        LoginContext context = new LoginContext();
        setValue(value, context);
        return context;
    }
    public static void setLoginContext(com.abc.common.web.context.LoginContext context){
        CONTEXT_HOLDER.set(context);
    }

    public static LoginContext getLoginContext(){
        return CONTEXT_HOLDER.get();
    }

    public static void remove(){
        CONTEXT_HOLDER.remove();
    }

    public static void setSubmitIp(String submitIp){
        SUBMIT_IP_HOLDER.set(submitIp);
    }
    public static String getSubmitIp(){
        return SUBMIT_IP_HOLDER.get();
    }

    protected static void setValue(String value, LoginContext context) {
        if (StringUtils.isNotEmpty(value)) {
            String[] fields = value.split(",");
            for (String keyValues : fields) {
                String[] keyValue = keyValues.split("=");
                if (keyValue.length == 2) {
                    try {
                        String field = keyValue[0];
                        if (StringUtils.isNotBlank(field)) {
                            BeanUtils.setProperty(context, field, StringEscapeUtils.unescapeCombinedCookie(keyValue[1]));
                        }
                    } catch (Exception e) {
                        log.warn("praser error!", e);
                    }
                }
            }
        }
    }

    /**
     * 判断是否登录。标准：userid > 0 and trim(pin).length > 0
     * @return
     */
    public boolean isLogin() {
        return StringUtils.isNotBlank(pin);
    }

    /**
     * 构造上下文
     * @param context
     * @return
     */
    public static String format(LoginContext context) {
        StringBuffer sb = new StringBuffer();
        sb.append("userId=")
                .append(context.getUserId())
                .append(",pin=")
                .append(context.getPin())
                .append(",nick=")
                .append(context.getNick());
        return sb.toString();
    }

}
